<!-- 订单列表 -->
<template>
   <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
            <el-breadcrumb-item>订单列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片 -->
        <el-card>
            <!-- 搜索框 -->
            <el-col :span="8">
                <el-input placeholder="请输入内容" v-model="queryParams.query" clearable @clear="getOrderList">
                    <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
                </el-input>
            </el-col>
            <!-- 商品列表 -->
            <el-table
                :data="orderList"
                border
                stripe
                style="width: 98%">
                <el-table-column type="index" label="#" ></el-table-column>
                <el-table-column prop="order_id" label="订单编号" ></el-table-column>
                <el-table-column prop="order_price" label="订单价格" width="120px"></el-table-column>
                <el-table-column prop="pay_status" label="是否付款" width="120px">
                    <template slot-scope="scope">
                        <el-tag type="danger" v-if="scope.row">未付款</el-tag>
                        <el-tag type="success" v-else>已付款</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="is_send" label="是否发货" width="100px"></el-table-column>
                <el-table-column prop="create_time" label="下单时间" width="200px"></el-table-column>
                <el-table-column label="操作" width="200px">
                    <template slot-scope="scope">
                        <!-- 修改 -->
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="showTimeLineDialog(scope.row.order_id)" ></el-button>
                        <!-- 删除 -->
                        <template>
                            <el-button type="success" icon="el-icon-location" size="mini" @click="showTimeLineDialog(scope.row.order_id)" disabled></el-button>
                        </template>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryParams.pagenum"
                :page-sizes="[10, 15, 20]"
                :page-size="queryParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="this.total">
            </el-pagination>
        </el-card>

        <!-- 物流进度对话框 -->
        <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
            <!-- time line -->
            <el-timeline :reverse="reverse">
                <el-timeline-item
                    v-for="(activity, index) in activities"
                    :key="index"
                    :timestamp="activity.timestamp">
                    {{activity.content}}
                </el-timeline-item>
            </el-timeline>
        </el-dialog>
   </div>
</template>

<script>
    export default {
        data () {
            return {
                // 订单列表
                orderList: [],
                // 查询参数
                queryParams: {
                    query: '',
                    pagenum: 1,
                    pagesize: 10
                },
                // 订单总数
                total: 0,
                // 展示物流进度对话框
                progressVisible: false,
                // 时间线倒序
                reverse: true,
                // 物流信息
                activities: []
            };
        },
        created() {
            this.getOrderList()
        },
        methods: {
            // 获取订单列表
            getOrderList: async function() {
                const {data: res} = await this.$http.get('orders', {params:this.queryParams})
                console.log('获取菜单的返回数据', res);
                if (res.meta.status !== 200) {
                    return this.$message.error('获取订单列表失败！')
                }
                this.orderList = res.data.goods
                this.orderList.forEach(item => {
                    item.create_time = this.getLocalTime(item.create_time)
                })
                this.total = res.data.total
                this.$message.success('成功获取订单列表')
                console.log('orderList: ', this.orderList);
            },
            // 
            handleSizeChange: function() {

            },
            // 
            handleCurrentChange: function() {

            },
            // 时间戳 -> 格式化日期
            getLocalTime: function(nS) {     
                return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');     
            },
            // 展示物流对话框
            showTimeLineDialog: async function(id) {
                const {data: res} = await this.$http.get(`/kuaidi/${id}`)
                console.log('物流信息: ', res);
                if (res.meta.status !== 200) {
                    return this.$$message.error('获取物流信息失败')
                }
                
                this.activities = res.data
                this.progressVisible = true
            }
        }
    }

</script>
<style lang='less' scoped>
.el-col {
    margin-bottom: 15px;
}
</style>